﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace TestGame {
    /// <summary>
    /// A PeerSessionScreen is used to determine if the user will host or join
    /// a PeerSession game. Depending on the option, different ports and 
    /// connection methods will be used.
    /// </summary>
    class PeerSessionScreen : MenuScreen {
        #region Fields

        MenuEntry hostEntry;
        MenuEntry joinEntry;
        MenuEntry backEntry;

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a PeerSessionScreen.
        /// </summary>
        public PeerSessionScreen() : base("Peer Session") {
            hostEntry = new MenuEntry("Host Game");
            joinEntry = new MenuEntry("Join Game");
            backEntry = new MenuEntry("Back");

            hostEntry.Selected += OnHostSelected;
            joinEntry.Selected += OnJoinSelected;
            backEntry.Selected += OnCancel;

            MenuEntries.Add(hostEntry);
            MenuEntries.Add(joinEntry);
            MenuEntries.Add(backEntry);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Is called when the "Host Game" menu entry is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnHostSelected(object sender, PlayerIndexEventArgs e) {
            IPEndPoint info = new IPEndPoint(IPAddress.Any, MyGame.Port);
            PeerMultiplayerScreen screen = new PeerMultiplayerScreen(info, true);

            ScreenManager.AddScreen(screen, ControllingPlayer);
        }

        /// <summary>
        /// Is called when the "Join Game" menu entry is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnJoinSelected(object sender, PlayerIndexEventArgs e) {
            ConnectionScreen screen = new ConnectionScreen();

            ScreenManager.AddScreen(screen, ControllingPlayer);
        }

        #endregion
    }
}
